var e = getApp(),
  a = e.requirejs("core");
Page({
  data: {
    icons: e.requirejs("icons"),
    member: {},
    show: false,
    background: e.config.background
  },
  onShow: function () {
    var that = this,
      i = getApp().getCache("userinfo");
    if (i) {
      a.get('Shop/info',{
        uid:i.uid
      },function(t){
        if(t.code==0){
          e.globalData.shop_id = t.info.shop_id;
          that.setData({
            info:t.info,
            show:true
          })
        }else{
          a.alert(t.msg);
        }
      },!that.data.show)
    } else {
      a.alert('请先登录');
    }
  },
  my_code: function () {
    a.jump('/pages/my_code/index')
  },
  /**
* 下拉刷新
*/
  onPullDownRefresh: function () {

    wx.stopPullDownRefresh();
  },
  scancode:function(){
    var that = this;
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        console.log(res)
        if (res.errMsg == "scanCode:ok" && res.result && res.result != '') {
        
          var data = JSON.parse(unescape(res.result));
          var timestamp = Date.parse(new Date()) / 1000;
          console.log(data);
          if (!data['add_staff']){
            a.alert('请扫员工二维码');
            return;
          }
          if (timestamp<data['over_time']){
            a.confirm('确定要添加该员工吗',function(){
              a.get('Shop/add_staff',{
                openid: data['id'],
                shop_id:that.data.info.shop_id,
                uid: getApp().getCache('userinfo').uid
              },function(t){
                if(t.code==0){
                  a.success('添加成功');
                  setTimeout(function(){
                    a.jump('/pages/staff/index');
                  },true)
                }else{
                  a.alert(t.msg);
                }
              },true)
            })  
          }else{
            a.alert('该二维码已失效!');
          }
        }else {
          a.alert('扫码失败请重试');
          return;
        }
      },
      fail: function () {
       // a.alert('扫码失败');
      }
    })
  },
 
  url:function(){
    a.jump('/pages/staff/index');
  },

  //分享
  onShareAppMessage: function () {
    return r.onShareAppMessage('pages/user/index')
  },

})